package com.adcc.foc.controller;

import com.adcc.foc.domain.ResultState;
import com.adcc.foc.utils.HttpClientUtil;
import com.adcc.foc.utils.Log;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by  zhangcece
 */
@Controller
public class MessageController extends BaseController {

    @RequestMapping(value = "/systemMessageManager/getPersonMessageCount", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findTrainById(String id, HttpServletRequest request, HttpSession session) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //调用iqs消息接口
        Gson gson = new Gson();
        String authUserId = (String) session.getAttribute("loginUserId");
        Properties properties = new Properties();
        properties.load(this.getClass().getClassLoader().getResourceAsStream("conf.properties"));
        String url = properties.getProperty("flightNotice");
        try {
            String responseContent = HttpClientUtil.getInstance().sendHttpPost(url, "authUserId=" + authUserId);
            JsonObject returnData = new JsonParser().parse(responseContent).getAsJsonObject();
            if (returnData.get("msg").toString().substring(1, 8).equals("success")) {
                String count = returnData.get("count").toString();
                map.put("count", count);
                map.put("status", ResultState.SUCCESS);
            } else {
                map.put("status", ResultState.ERROR);
            }
        } catch (Exception e) {
            Log.error(ExceptionUtils.getFullStackTrace(e), MessageController.class);
            map.put("status", ResultState.ERROR);
        }
        return map;
    }

}
